<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<chiptrack:checkLogon sourcePage="/taxAndTerms.do"/>
<%@ page import="java.util.HashMap,
				 java.util.Iterator,java.util.ArrayList,
				 com.scalar.chiptrack.sales.TermsAndConditions,
				 com.scalar.chiptrack.utils.ConfigManager,
				 com.scalar.chiptrack.users.Functionality,
				 com.scalar.chiptrack.users.User,
				 com.scalar.chiptrack.commons.Constants"%>
<%
    String chipTracContext = "/" + ConfigManager.getChipTracContext();
    int count = 0;
    int termscount =0;
%>


<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>

<script language="JavaScript">
function addMoreText()
{
    var formName = document.taxForm;
    var newcount = formName.newcount.value;
    var htmltext = '<div><table border="0" width="100%">';
    htmltext = htmltext + '<tr><td align=center bgcolor="#FFFFEE" class="NArialL"><input type="text" name="state" value="" maxlength="20" class="txfield"></td>';
    htmltext = htmltext + '<td align="center" bgcolor="#FFFFEE" class="NArialL"><input type="text" name="tax" value="" class="valuetxfield" maxlength="6" onkeypress="javascript:restrictCharacters(this,3,3)" onkeyup="javascript:restrictAmt(this,3,3)">';
    htmltext = htmltext + '<input type="hidden" name="type" value="new"><input type="hidden" name="oldState" value=""><input type="hidden" name="oldTax" value="0.0"></td></tr></table></div>';
    var pa=document.getElementById("font1");
    pa.insertAdjacentHTML('BeforeEnd',htmltext);
    newcount++;
    formName.newcount.value = newcount;
}

function addMoreText1()
{
    var formName = document.taxForm;
    var newtermscount = formName.newtermscount.value;
    var htmltext = '<div><table border="0" width="100%">';
    htmltext = htmltext + '<tr><input type="hidden" name="termsId" value="-1"><td align=center bgcolor="#FFFFEE" class="NArialL"><input type="text" name="title" value="" class="txfield" size="30"></td>';
    htmltext = htmltext + '<td align="center" bgcolor="#FFFFEE" class="NArialL"><textarea name="terms" value="" class="txfield" cols="60" rows="4"></textarea></td>';
    htmltext = htmltext + '</tr></table></div>';
    var pa=document.getElementById("font2");
    pa.insertAdjacentHTML('BeforeEnd',htmltext);
    newtermscount++;
    formName.newtermscount.value = newtermscount;
}

function submitPage()
{
    if( (checkStateNames() == true) && (checkTerms() == true) )
    {
      document.taxForm.action.value="Save";
      document.taxForm.submit();
    }
}
function checkStateNames()
{
    var formName = document.taxForm;
    var pa = document.getElementById("message");
    var noOfStates = (parseInt(formName.count.value) + parseInt(formName.newcount.value));

    if(noOfStates==1)
    {
        var stateName = formName.state.value.trim();
        var taxValue = formName.tax.value.trim();

        formName.state.value = stateName;
        formName.tax.value = taxValue;

        if(stateName=="" && taxValue=="")
        {
            taxValue = "0.0";
            return true;
        }
        if(stateName=="")
        {
            pa.innerHTML = "<li> Please enter a valid State ";
            formName.state.focus();
            formName.state.select();
            return false;
        }
        if(taxValue=="")
        {
            pa.innerHTML = "<li> Tax cannot be blank ";
            formName.tax.focus();
            formName.tax.select();
            return false;
        }
        if(  stateName.length != 0 && stateName != '' )
        {
            if( !(is_Sp_Characters_State(stateName)) || (check_char_position5(stateName)) )
            {
                pa.innerHTML = "<li> Please enter a valid state  ";
                formName.state.focus();
                formName.state.select();
                return false;
            }
        }
        if( taxValue!="" && taxValue <=0 )
        {
            pa.innerHTML = "<li> Tax cannot be less than or equal to zero";
            formName.tax.focus();
            formName.tax.select();
            return false;
        }
        if( checkFloatPrice(taxValue,"tax","Y") == false)
        {
            pa.innerHTML = errMsg;
            formName.tax.focus();
            formName.tax.select();
            return false;
        }
    }
    else
    {
        for(var i=0; i<parseInt(noOfStates); i++)
        {
            var stateName = formName.state[i].value.trim();
            var taxValue = formName.tax[i].value.trim();
            formName.state[i].value = stateName;
            formName.tax[i].value = taxValue;

            if(stateName=="" && taxValue=="")
            {
                formName.tax[i].value = "0.0";
                continue;
            }
            if(stateName=="")
            {
                pa.innerHTML = "<li> Please enter a valid State ";
                formName.state[i].focus();
                formName.state[i].select();
                return false;
            }
            if(taxValue=="")
            {
                pa.innerHTML = "<li> Tax cannot be blank ";
                formName.tax[i].focus();
                formName.tax[i].select();
                return false;
            }
            if(  stateName.length != 0 && stateName != '' )
            {
                if( !(is_Sp_Characters_State(stateName)) || (check_char_position5(stateName)) )
                {
                    pa.innerHTML = "<li> Please enter a valid state  ";
                    formName.state[i].focus();
                    formName.state[i].select();
                    return false;
                }
            }
            if( taxValue!="" && taxValue<=0 )
            {
                pa.innerHTML = "<li> Tax cannot be less than or equal to zero";
                formName.tax[i].select();
                return false;
            }

            if( checkFloatPrice(taxValue,"tax","Y") == false)
            {
                pa.innerHTML = errMsg;
                formName.tax[i].focus();
                formName.tax[i].select();
                return false;
            }
            for(var j=(parseInt(i)+1);j<parseInt(noOfStates);j++ )
            {
                if(i!=j)
                {
                    if(stateName.toLowerCase() == formName.state[j].value.trim().toLowerCase())
                    {
                        pa.innerHTML = "<li> '"+ formName.state[j].value +"' already exists in the system" ;
                        formName.state[j].focus();
                        formName.state[j].select();
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

function showErrorReportPage()
{
    newWindow = window.open ('<%=chipTracContext%>/reportProblem.do','reportProblem','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=750,height=400,resizable=no');
    newWindow.focus();
}

function checkTerms()
{
    var formName = document.taxForm;
    var pa=document.getElementById("message");
    var noOfTerms = (parseInt(formName.termscount.value) + parseInt(formName.newtermscount.value));

    if(noOfTerms==1)
    {
        var title = formName.title.value.trim();
        var terms=formName.terms.value.trim();
        formName.title.value = title;
        formName.terms.value = terms;

        if(title=="" && terms=="")
        {
            return true;
        }

        if(title=="")
        {
            pa.innerHTML = "<li> Please enter a valid Title";
            formName.title.focus();
            formName.title.select();
            return false;
        }
        if(terms=="")
        {
            pa.innerHTML = "<li> Please enter valid terms";
            formName.terms.focus();
            formName.terms.select();
            return false;
        }
    }
    else
    {

        for(var i=0; i<parseInt(noOfTerms); i++)
        {
            var title = formName.title[i].value.trim();
            var terms=formName.terms[i].value.trim();
            formName.title[i].value = title;
            formName.terms[i].value = terms;

            if(title=="" && terms=="")
            {
                continue;
            }
            if(title=="")
            {
                pa.innerHTML = "<li> Please enter a valid Title";
                formName.title[i].focus();
                formName.title[i].select();
                return false;
            }
            if(terms=="")
            {
                pa.innerHTML = "<li> Please enter valid terms";
                formName.terms[i].focus();
                formName.terms[i].select();
                return false;
            }
            for(var j=(parseInt(i)+1);j<parseInt(noOfTerms);j++ )
            {
                if(i!=j)
                {
                    if(title.toLowerCase() == formName.title[j].value.trim().toLowerCase())
                    {
                        pa.innerHTML = "<li> '" + formName.title[j].value +"' already exists in the system";
                        formName.title[j].focus();
                        formName.title[j].select();
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
</script>
</HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TR>
	<td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
        <!-- Side Navigation start -->
	    <%@ include file="/jsp/sales/sales_order_menu.jsp" %>
        <!-- end of the side navigations -->
    </td>
    <TD bgcolor="#ffffff"><IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
    <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
    <TD valign="top" width="100%">
        <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
        <BR>
        <html:form name="taxForm" method="post" action="/taxAndTerms.do" type="com.scalar.chiptrack.ChipTrackActionForm">
        <center><span class="NArialW"> Sales Tax and Terms Setup</span></center>
        <img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10>
        <table border="0" width="90%" align=right>
            <tr>
                <td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td>
            </tr>
            <% String sss = (String)request.getAttribute ( "showMessage" );
            if (sss.equalsIgnoreCase( "TRUE" ) ) { %>
            <tr><td colspan=7 align='left'><font class='NArialS'><font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b></font></font></td></tr>
            <% } %>
            <tr><td class="NArialL"><font color="FF0000"><chiptrack:errors styleClass="NArialL"/></font></td></tr>
            <tr><td class="NArialL"><font color="FF0000"><chiptrack:info styleClass="NArialL" /></font></tr>
            <tr><td class='NArialL'><FONT id="message" color="red">&nbsp;</FONT></TD></TR>
            <tr><td class="NArialL">Sales Tax</td></tr>
            <tr>
                <td>
                    <table border="0" width="85%" bordercolor="#FFFFFF">
	                    <tr>
                            <td align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">State</font></font></td>
                            <td align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Tax(%)</font></font></td>
                        </tr>
                        <%
                        HashMap map = (HashMap) request.getAttribute("taxList");
                        Iterator states = map.keySet().iterator();
                        Iterator taxes = map.values().iterator();
                        while(states.hasNext())
                        {
                            String state = (String)states.next();
                            String tax = (String)taxes.next();
                            count++;
                         %>
                        <tr>
                            <td align="center" bgcolor='#FFFFEE' class="NArialL">
                                <input type="text" name="state" value="<%=state%>" maxlength ="20" class="txfield">
                                <input type="hidden" name="oldState" value="<%=state%>">
                            </td>
                            <td align="center" bgcolor='#FFFFEE' class="NArialL">
                                <input type="text" name="tax" value="<%=tax%>" class="valuetxfield" maxlength="6" onkeypress="javascript:restrictCharacters(this,3,3)" onkeyup="javascript:restrictAmt(this,3,3)">
                                <input type="hidden" name="oldTax" value="<%=tax%>">
                                <input type="hidden" name="type" value="old">

                            </td>
                        </tr>
                        <%
                        }
                        %>
	                    <tr><td colspan=2><FONT id="font1"></FONT></TD></TR>
                    </table>
                </td>
            </tr>
            <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'  ></td></tr>
            <tr>
                <td align=right>
                    <a href="javascript:addMoreText();" onmouseover="window.status='Click to Add Tax';return true" onmouseout="window.status='';return true" title="Click here to Add Tax" ><img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border='0'></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
            <tr><td class="NArialL">Terms and Conditions</td></tr>
            <tr>
                <td>
                    <table border="0" width="85%" bordercolor="#FFFFFF">
                        <tr>
                            <td align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Title</font></font></td>
                            <td align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Terms and Conditions</font></font></td>
                        </tr>

                        <%
                         ArrayList list = (ArrayList) request.getAttribute("termsList");
                        for (int i=0; i<list.size(); i++)
                        {
                            TermsAndConditions termsC = (TermsAndConditions) list.get(i);
                            int termsId = termsC.getTermsId();
                            String title = termsC.getTermsTitle();
                            String terms = termsC.getTerms();
                        %>
                        <tr>
                            <td align="center" bgcolor='#FFFFEE' class="NArialL">
                                <input type="hidden" name="termsId" value="<%=termsId%>">
                                <input type="text" name="title" value="<%=title%>" class="txfield" size='30'>
                            </td>
                            <td align="center" bgcolor='#FFFFEE' class="NArialL"><textarea name="terms" value="<%=terms%>" class="txfield" cols='60' rows='4'><%=terms%></textarea></td>
                        </tr>
                        <%
                            termscount++;
                        }
                        %>
                        <tr><td colspan=2><FONT id="font2"></FONT></TD></TR>
                    </table>
                </td>
            </tr>
            <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
            <tr>
                <td align=right>
                    <a href="javascript:addMoreText1();" onmouseover="window.status='Click to Add More Terms and Conditions';return true" onmouseout="window.status='';return true" title="Click here to Add More Terms and Conditions"><img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border=0></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
            <tr>
                <td align=center>
                    <chiptrack:restrictedLink functionality='<%=Functionality.SALES_TAX_TERMS_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:submitPage();" onmouseover="window.status='Click to save sales tax & terms';return true" onmouseout="window.status='';return true"><img src="<%=chipTracContext%>/jsp/includes/images/save.gif" border='0'title="Click here to save sales tax & terms"></chiptrack:restrictedLink>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="javascript:document.taxForm.reset();" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset" ><img src='<%=chipTracContext%>/jsp/includes/images/reset.gif' border=0></a>
                </td>
            </tr>
        </table>
        <input type="hidden" name="action" value="">
        <input type="hidden" name="count" value="<%=count%>">
        <input type="hidden" name="newcount" value="0">
        <input type="hidden" name="termscount" value="<%=termscount%>">
        <input type="hidden" name="newtermscount" value="0">
        </html:form>
        <BR><BR><BR>
    </TD>
    <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</HTML>
